package com.atguigu.dwm.plan.controller;

import com.atguigu.dwm.common.bean.Result;
import com.atguigu.dwm.plan.bean.DpDataDomain;
import com.atguigu.dwm.plan.bean.DpDataWarehouseModel;
import com.atguigu.dwm.plan.service.DpDataDomainService;
import com.atguigu.dwm.plan.service.DpDataWarehouseModelService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 数据域 前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2024-07-03
 */
@RestController
@RequestMapping("/data-plan/domain")
public class DpDataDomainController {

    @Autowired
    DpDataDomainService dpDataDomainService;

    @Autowired
    DpDataWarehouseModelService dpDataWarehouseModelService;


    @GetMapping("/list")
    public Result  getList(@RequestParam(value = "modelId",required = false) Long modelId){
        QueryWrapper<DpDataDomain> queryWrapper = new QueryWrapper<DpDataDomain>().eq(modelId != null, "model_id", modelId)
                .eq("is_deleted", "0");

        //不要循环查询数据库
        //        List<DpDataDomain> list = dpDataDomainService.list(queryWrapper);
//        for (DpDataDomain dpDataDomain : list) {
//            DpDataWarehouseModel warehouseModel = dpDataWarehouseModelService.getById(dpDataDomain.getModelId());
//            dpDataDomain.setModelName(warehouseModel.getModelName());
//        }
        List<DpDataDomain> domainList = dpDataDomainService.getDomainList(modelId);
        return Result.ok(domainList);

    }

    @PostMapping("/detail")
    public Result saveDomain(@RequestBody DpDataDomain dpDataDomain){
        dpDataDomain.setLastUpdateTime(new Date());
        dpDataDomain.setLastUpdateUserId(9999L);
        dpDataDomainService.saveOrUpdate(dpDataDomain);//保存后 会把主键补充在id上
        return Result.ok(dpDataDomain);
    }

    @GetMapping("/detail/{id}")
    public Result getDetail(@PathVariable("id") Long id){
        return Result.ok(dpDataDomainService.getById(id));
    }

    @GetMapping("/options")
    public Result getOptions(@RequestParam(value = "modelId",required = false) Long modelId){
        List<Map<String, Object>> mapList = dpDataDomainService.listMaps(new QueryWrapper<DpDataDomain>()
                        .eq(modelId!=null , "model_id", modelId)
                .select("id", "name_chn as name", "name_eng as nameEng").eq("is_deleted", "0"));
        return Result.ok(mapList);
    }

}
